# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import InterfaceUtils
import NewCategoryDialog

class EditCategoryDialog(NewCategoryDialog.NewCategoryDialog):
	def __init__(self, parentWindow, account, editCategoryCb, categoryName):
		NewCategoryDialog.NewCategoryDialog.__init__(self, parentWindow, account, editCategoryCb)
		self.window.set_title("Edit Category")
		
		self.category = self.account.findCategory(categoryName)
		self.nameEntry.set_text(self.category.name)
		
		for index, person in enumerate(self.account.getPeople()):
			if self.category.getPaidBy() == person:
				#the first index is always "Anyone"
				self.paidByCombo.set_active(index + 1)
		
		self.shareWidget.SetProfile(self.category.getSharedBy())
	
	def OnOk(self, widget):
		name = self.nameEntry.get_text()
		paid = self.paidByCombo.get_active_text()
		shareProfile = self.shareWidget.GetProfile()
		
		msg = None
		if not name:
			msg = "You must provide a name for the category."
		elif len(shareProfile) == 0:
			msg = "Please select at least one person to share this category."
		
		if msg:
			InterfaceUtils.ShowErrorDialog(self.window, msg)
			return
		
		if paid == "Anyone":
			paid = None
		
		self.addCategoryCb(self.category.getName(), name, paid, shareProfile)
		
		self.window.destroy()
